package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * 定时上传参数页面
 * @author: 古霸天
 * @date: 2020/12/14
 */
public class TimedUploadParamFragment extends AbstractProtocolFragment {

    private final String[] dayOrMonth = {"天", "月"};

    /**
     * 天数/月份
     */
    private Spinner dayMonthSpinner;
    /**
     * 周期值输入框
     */
    private EditText periodTxt;
    /**
     * 时间输入框
     */
    private EditText hourTxt;
    /**
     * 分钟输入框
     */
    private EditText minuteTxt;

    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public TimedUploadParamFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_timed_upload_param, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化
     * @param v
     */
    private void init(View v) {
        ArrayAdapter trueOrFalseAdapter = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_item, dayOrMonth);
        trueOrFalseAdapter.setDropDownViewResource(R.layout.dropdown_stytle);
        dayMonthSpinner = v.findViewById(R.id.spinner_day_month);
        dayMonthSpinner.setAdapter(trueOrFalseAdapter);

        // 定义输入框过滤器
        InputFilter[] inputFilters = new InputFilter[1];
        // 设置输入框输入内容长度
        inputFilters[0] = new InputFilter.LengthFilter(2);

        periodTxt = v.findViewById(R.id.timed_upload_param_period);
        periodTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
        periodTxt.setFilters(inputFilters);

        hourTxt = v.findViewById(R.id.timed_upload_param_hour);
        hourTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
        hourTxt.setFilters(inputFilters);

        minuteTxt = v.findViewById(R.id.timed_upload_param_minute);
        minuteTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
        minuteTxt.setFilters(inputFilters);
    }

    @Override
    public void fillFragment(String data) {
        // 清空输入框
        periodTxt.setText("");
        hourTxt.setText("");
        minuteTxt.setText("");
        Runnable r = () -> {
            reader(Nb25ProtocolUtils.formatReceiveData(data));
        };
        activity.getHandler().postDelayed(r, 200);
    }
    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return getData();
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        dayMonthSpinner.setSelection(StringUtils.equals("00", data.substring(0, 2)) ? 0 : 1);
        periodTxt.setText(String.valueOf(Integer.parseInt(data.substring(2, 4), 16)));
        hourTxt.setText(data.substring(4, 6));
        minuteTxt.setText(data.substring(6, 8));
    }
    /**
     * 获取数据域
     * @return
     */
    private String getData() {

        StringBuffer data = new StringBuffer();

        String period = periodTxt.getText().toString();
        if (StringUtils.isEmpty(period)) {
            Toast.makeText(activity, "周期值不能为空", Toast.LENGTH_SHORT).show();
            return data.toString();
        }
        String hour = hourTxt.getText().toString();
        if (StringUtils.isEmpty(hour)) {
            Toast.makeText(activity, "天数不能为空", Toast.LENGTH_SHORT).show();
            return data.toString();
        }
        String minute = minuteTxt.getText().toString();
        if (StringUtils.isEmpty(minute)) {
            Toast.makeText(activity, "分钟不能为空", Toast.LENGTH_SHORT).show();
            return data.toString();
        }

        data.append((dayMonthSpinner.getSelectedItemPosition() == 0 ? "00" : "01"));
        data.append(HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(period)), 2).toUpperCase());
        data.append(HexUtils.paddingLeft(hour, 2));
        data.append(HexUtils.paddingLeft(minute, 2));

        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), data.toString(), PROTOCOL_VERSION);
    }
}
